package pl.edu.romanow.query

import javax.xml.crypto.Data
import com.tinkerpop.gremlin.scala.GremlinScalaPipeline
import pl.edu.romanow.util.AppException
import pl.edu.romanow.model.Database._
import com.tinkerpop.blueprints.pgm.{Edge, Vertex}
import scala.collection.mutable.Set;
import scala.collection.JavaConversions._
import pl.edu.romanow.model.{Facebook, Database, GraphVisualisationUtil}

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 29.04.12
 * Time: 11:38
 * To change this template use File | Settings | File Templates.
 */

case class Query(var startId: String, var items: List[QueryItem], takeFirst: Boolean = true, source: String = "Facebook") {


  def drawQuery: (String, Boolean) = {

    if (items.size < 1) {
      throw AppException("empty query");
    }


    val edgeSet = edgesSet


    GraphVisualisationUtil.displayGraph(edgeSet.toSeq)
  }

  def edgesSet = {
    val edgeSet = Set[Edge]()
    if (startId == "me") {
      startId = Facebook.currentUser.open_!.id
    }
    val q = items.reverse.foldLeft(G(Database.appCon.findEdgeByVId(source, startId).openOr(throw AppException("bad ID")))) {
      (pipe, qi) =>
        qi.edges(pipe, edgeSet)
    }.toList
    edgeSet
  }

}
